<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>Gallery缩略图制作</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Faster.html">When perl is not quite fast enough</a>&nbsp;&nbsp;>>Next: <a href="IPaddress.html">如何寻找用户真实IP</a></p>
<h1>Gallery缩略图制作</h1>
<div class='content'>
<p>Category: <a href='Script.html'>Script</a> &nbsp; Keywords: <b>thumbnails Gallery</b></p><h2>描述</h2>
此文档用以生成某目录下所有图片的缩略图，并可生成索引/index<br>
<a href="../scripts/gallery.pl.txt">Download gallery.pl</a>

<h2>先决条件</h2>
use <a href="http://www.imagemagick.com">Image::Magick</a>;

<h2>Script</h2>
<pre><code>
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use Image::Magick;

#settings
my $thumbs_width = 100; #thumbs's width
my $thumbs_height = 100; #thumbs's height
my $dir = 'E:/Fayland/Gallery'; #the to-do dir
my $url = 'http://localhost/Gallery'; #the relevant url
my $need_index = 1; # 1 - produce the index.html, 0 - ingore

my $query = new CGI;
print $query->header;

my @gallery;
(-d "$dir") or die "Cann't find $dir";
opendir(DIR,"$dir");
@gallery = readdir(DIR);
closedir(DIR);
@gallery = grep(/(gif|jpe?g|png)$/, @gallery);

my $i;
open(FH, ">$dir/index.html") if ($need_index);
foreach (@gallery) {
    $_ =~ m/(.*).(.*)/;
    my $thumbsfile = "$1.png";
    unless (-e "$dir/thumbs/$thumbsfile") {
        my $image=Image::Magick->new;
        my $x = $image->Read("$dir/$_");
        warn "$x" if "$x";
        $x = $image->Resize(width=>$thumbs_width, height=>$thumbs_height);
        $x = $image->Write("$dir/thumbs/$thumbsfile");
    }
    print "&lt;a href='$url/$_'>&lt;img src='$url/thumbs/$thumbsfile' width='100' height='100' border='0' alt='$_' />&lt;/a>";
    print FH "&lt;a href='$_'>&lt;img src='thumbs/$thumbsfile' width='100' height='100' border='0' alt='$_' />&lt;/a>" if ($need_index);
    $i++;
    if ($i % 5 == 0) {
        print "&lt;br>";
        print FH "&lt;br>" if ($need_index);
    }
}
close(FH) if ($need_index);

print "&lt;p>Visit the &lt;a href='$url/index.html'>IndexPage&lt;/a>&lt;/p>" if ($need_index);
</code></pre>

<h2>License</h2>
The same as Perl.</div>
<p><&lt;Previous: <a href="Faster.html">When perl is not quite fast enough</a>&nbsp;&nbsp;>>Next: <a href="IPaddress.html">如何寻找用户真实IP</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=Gallery%E7%BC%A9%E7%95%A5%E5%9B%BE%E5%88%B6%E4%BD%9C&url=http://www.fayland.org/journal/Gallery.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='DBI.html'>DBI</a> < <span class='digit'>2004-10-30 18:50:50</span> ></li><li><a href='050330.html'>Class::DBI 改 Primary 项</a> < <span class='digit'>2005-03-30 22:27:07</span> ></li></ul>
Created on <span class="digit">2004-10-30 19:03:51</span>, Last modified on <span class="digit">2005-06-02 01:51:45</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>